package com.gxa.controller;

import com.gxa.pojo.CustomerType;
import com.gxa.pojo.Supplier;
import com.gxa.service.SupplierService;
import com.gxa.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/supplier")
@Api(value = "执行供应商操作的控制器", tags = "供应商接口的api")
@CrossOrigin
public class SupplierController {

    @Autowired
    SupplierService supplierService;

    // 添加
    @ResponseBody
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "添加供应商的接口", notes = "供应商添加", httpMethod = "post", response = String.class)
    public Map insert(@RequestBody Map map) {
        int i = supplierService.insertSelective(map);
        JsonResult jsonResult = new JsonResult();
        if (i > 0) {
            jsonResult.setCode("200");
            jsonResult.setMsg("添加成功！");
        } else {
            jsonResult.setCode("406");
            jsonResult.setMsg("添加失败！");
        }
        return jsonResult.getValues();
    }

    // 删除
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除供应商的接口", notes = "供应商删除", httpMethod = "post", response = String.class)
    public Map delete(@RequestBody Map<String, Object> s) {
        Integer id = (Integer) s.get("id");
        int i = supplierService.deleteByPrimaryKey(id);
        JsonResult jsonResult = new JsonResult();
        if (i > 0) {
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功！");
        } else {
            jsonResult.setCode("406");
            jsonResult.setMsg("删除失败！");
        }
        return jsonResult.getValues();
    }

    // 批量删除
    @ResponseBody
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
    @ApiOperation(value = "批量删除供应商的接口", notes = "供应商批量删除", httpMethod = "post", response = String.class)
    public Map deleteBatch(@RequestBody Map<String, List> s) {
        List ids = s.get("id");
        int i = supplierService.deleteBatch(ids);
        JsonResult jsonResult = new JsonResult();
        if (i > 0) {
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功！");
        } else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开小差了！");
        }
        return jsonResult.getValues();
    }

    // 修改
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "修改供应商的接口", notes = "供应商修改", httpMethod = "post", response = String.class)
    public Map update(@RequestBody Map map) {
        Integer id = (Integer) map.get("id");
        map.remove("id");
        map.put("id", id);
        int i = supplierService.updateByPrimaryKeySelective(map);
        JsonResult jsonResult = new JsonResult();
        if (i > 0) {
            jsonResult.setCode("200");
            jsonResult.setMsg("修改成功！");
        } else {
            jsonResult.setCode("406");
            jsonResult.setMsg("修改失败！");
        }
        return jsonResult.getValues();
    }

    // 分页查询
    @ResponseBody
    @RequestMapping(value = "/findAllSupplierByPage", method = RequestMethod.POST)
    @ApiOperation(value = "查询供应商数据列表分页的接口", notes = "获取所有的供应商数据列表", httpMethod = "post", response = String.class)
    public Map findAllSupplierByPage(@RequestBody Map map) {
        String number = map.get("number").toString();     //当前页数 1
        String size = map.get("size").toString();         // 分页大小 5
        Integer offset = (Integer.parseInt(number) - 1) * Integer.parseInt(size);
        Integer limit = Integer.parseInt(size);     // limit=size
        List<Supplier> allSupplierByPage = supplierService.findAllSupplierByPage(offset, limit);
        Integer count = supplierService.findAllSupplier();// 数据的总条数
        //计算总页数
        int totalPage = count / limit;
        if (count % limit == 0) {
            totalPage = count / limit;
        } else {
            totalPage = count / limit + 1;
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(allSupplierByPage);
        //jsonResult.setData("totalPage:"+totalPage);
        jsonResult.put("count", count);
        jsonResult.put("totalPage", totalPage);
        return jsonResult.getValues();
    }

    // 多参数模糊查询      计算总条数
    @ResponseBody
    @RequestMapping(value = "/findBySelect", method = RequestMethod.POST)
    @ApiOperation(value = "多参数模糊查询供应商的接口", notes = "供应商多参数模糊查询", httpMethod = "post", response = String.class)
    public Map<String, Object> findBySelect(@RequestBody Map<String, Object> map) {
        JsonResult jsonResult = new JsonResult();
        if (map.get("supplierName") != null && map.get("supplierName").equals("") && map.get("contactman") != null && map.get("contactman").equals("")) {

            try {
                String number = map.get("number").toString();     //当前页数 1
                String size = map.get("size").toString();         // 分页大小 5
                Integer offset = (Integer.parseInt(number) - 1) * Integer.parseInt(size);
                Integer limit = Integer.parseInt(size);     // limit=size
                map.put("offset", offset);
                map.put("limit", limit);
                List<Map<String, Object>> bySelect = supplierService.findBySelect(map);
                Integer count = supplierService.countFindSupplier(map);
                jsonResult.setData(bySelect);
                jsonResult.put("count", count);
                jsonResult.setCode("200");
                jsonResult.setMsg("查询成功！");
            } catch (Exception e) {
                jsonResult.setCode("406");
                jsonResult.setMsg("系统开小差了！");
            }
        } else {
            try {
                String number = map.get("number").toString();     //当前页数 1
                String size = map.get("size").toString();         // 分页大小 5
                Integer offset = (Integer.parseInt(number) - 1) * Integer.parseInt(size);
                Integer limit = Integer.parseInt(size);     // limit=size
                map.put("offset", offset);
                map.put("limit", limit);
                List<Map<String, Object>> bySelect = supplierService.findBySelect(map);
                Integer count = supplierService.countFindSupplier(map);
                jsonResult.setData(bySelect);
                jsonResult.put("count", count);
                jsonResult.setCode("200");
                jsonResult.setMsg("查询成功！");
            } catch (Exception e) {
                jsonResult.setCode("406");
                jsonResult.setMsg("系统开小差了！");
            }
        }
        return jsonResult.getValues();

    }
}



